﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Experimental8.Common.Data.EDM;
using Experimental8.Infrastructure.DAL.Interfaces;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Experimental8.DAL.Services
{
    [Export(typeof(IDAInfrastructureService))]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
    public class DAInfrastructureService : IDAInfrastructureService
    {        
        private Experimental8ModelContainer _edm = Experimental8ModelContainer.Instance;

        public DAInfrastructureService()
        {
        }

        public ObservableCollection<ui_fields_information_item> GetUIfieldsInformationItemByCode(string uilinkname)
        {
            try
            {
                var query = (from fi in _edm.ui_fields_informationSet
                            .Include("ui_fields_information_items")
                             where fi.code == uilinkname
                             select fi).FirstOrDefault();
                return new ObservableCollection<ui_fields_information_item>(query.ui_fields_information_items.ToList<ui_fields_information_item>());
            }            
            catch (Exception ex)
            {
                Exception exceptionToThrow;
                if (ExceptionPolicy.HandleException(ex, "EntityExceptions Policy", out exceptionToThrow))
                {
                    if (exceptionToThrow == null)
                        throw;
                    else
                        throw exceptionToThrow;
                }
            }
            return null;            
        }         
    }
}
